package com.sensePlusVR.widget.dialog;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.sensePlusVR.activity.update.AppProgressActivity;
import com.sensePlusVR.base.BaseDialog;
import com.sensePlusVR.bean.AppVersionInfo;
import com.sensePlusVR.main.R;
import com.sensePlusVR.utils.MyDialogUtil;
import com.sensePlusVR.utils.MyLog;

public class AppVersionTipDialog extends BaseDialog implements android.view.View.OnClickListener{
	private static final String TAG = AppVersionTipDialog.class.getSimpleName();
	private Context context;
	
	//private TextView mFoundTv;
	
	private TextView versionCodeTv;
	
	private TextView mUpdateTipTv;
	
	private Button mUpdateBtn;
	
	private Button mCancelBtn;
	
	private Button mLaterBtn;
	

	
	private AppVersionInfo newAppVersionInfo;
	
	private boolean mIsShowLater;
	
	private boolean mForceUpdate;

	public AppVersionTipDialog(Context context,AppVersionInfo appVersionInfo,boolean isShowLater,boolean forceUpdate) {
		super(context,R.style.FullScreen_Dialog_Half_Tran);
		
		this.context = context;
		
		this.newAppVersionInfo = appVersionInfo;
		
		this.mIsShowLater = isShowLater;
		
		this.mForceUpdate = forceUpdate;
		
	}
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.dialog_update_tip);
		
		//mFoundTv = (TextView) findViewById(R.id.update_tip_found_version);
		
		versionCodeTv = (TextView) findViewById(R.id.update_tip_version_code);
		
		mUpdateTipTv = (TextView) findViewById(R.id.update_tip_iv);
		
		mUpdateBtn = (Button) findViewById(R.id.update_tip_udapte);
		
		mCancelBtn = (Button) findViewById(R.id.update_tip_del);
		
		mLaterBtn = (Button) findViewById(R.id.update_tip_later);
		
		mUpdateTipTv.setMovementMethod(ScrollingMovementMethod.getInstance());
		
		//强制升级隐藏部分UI
		
		mCancelBtn.setVisibility(mForceUpdate?View.INVISIBLE:View.VISIBLE);
		
		mLaterBtn.setVisibility(mForceUpdate?View.INVISIBLE:View.VISIBLE);
		
		//mFoundTv.setVisibility(mIsShowLater?View.VISIBLE:View.INVISIBLE);
		
		mLaterBtn.setVisibility(mIsShowLater?View.VISIBLE:View.INVISIBLE);
		
		
		mUpdateBtn.setOnClickListener(this);
		
		mCancelBtn.setOnClickListener(this);
		
		mLaterBtn.setOnClickListener(this);
		
		if(newAppVersionInfo!=null){
			MyLog.d(TAG, "newAppVersionInfo : " + newAppVersionInfo.toString());
			
			versionCodeTv.setText(newAppVersionInfo.getVersonname());
			
			mUpdateTipTv.setText(newAppVersionInfo.getDescription());
			
		}
		
			
		
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		//强制升级屏蔽返回键
		 if(keyCode==KeyEvent.KEYCODE_BACK)   
			 if(mForceUpdate){
				 
				 return true;
				 
			 }
			    return super.onKeyDown(keyCode, event);
	}
	
	@Override
	public void show() {
		// TODO Auto-generated method stub
		super.show();
		
		MyDialogUtil.setDialogFullScreen(context,this);
	}

	@Override
	public void onClick(View v) {
		
		switch(v.getId()){
		
		case R.id.update_tip_udapte:
			
			Intent i = new Intent(context,AppProgressActivity.class);
			
			context.startActivity(i);
			
			break;
			
		case R.id.update_tip_del:
		case R.id.update_tip_later:
			
			cancel();
			
			break;
		
		default:
			break;
		
		}
		
	}
	

	

}
